package nju.se.alibaba.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import nju.se.alibaba.dao.AlibabaDAO;
import nju.se.alibaba.model.Tmall;

public class Algorithm_hds_pre extends Algorithm{

	public HashMap<List<Integer>, Integer> readFile(String filename){
		HashMap<List<Integer>, Integer>  map = new HashMap<List<Integer>, Integer>();
		BufferedReader reader = null;
		try {
			FileReader outFile = new FileReader(new File("output.txt"));
			reader = new BufferedReader(outFile);
			String tmp=null;
			while(null!=(tmp = reader.readLine())){
				String[] tmplist = tmp.split(" ");
				int length = tmplist.length;
				if(length>3){
					List<Integer> brandIdList = new ArrayList<Integer>();
					for(int i=0;i<2;i++){
						brandIdList.add(Integer.parseInt(tmplist[i]));
					}
					map.put(brandIdList, Integer.parseInt(tmplist[length-1]));
				}
			}
			return map;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
	}
	public void run(){
		HashMap<List<Integer>, Integer>  map = readFile("output.txt");
		Set<List<Integer>> set = map.keySet();
		HashMap<Integer, HashSet<Integer>> result = new HashMap<Integer, HashSet<Integer>>();
		for (List<Integer> list : set) {
			HashMap<String, Integer> tmpmap = new HashMap<String, Integer>();
			tmpmap.put("in", list.get(0));
			tmpmap.put("notIn", list.get(1));
//			System.out.println(list.get(0));
//			System.out.println(list.get(1));
			List<Tmall> tmallList1 = AlibabaDAO.getPreBrand(tmpmap);
			HashSet<Integer> settmp1 = new HashSet<Integer>();
			settmp1.add(list.get(1));
			for (Tmall tmall : tmallList1) {
				result.put(tmall.getUser_id(), settmp1);
			}
			HashMap<String, Integer> tmpmap2 = new HashMap<String, Integer>();
			tmpmap2.put("in", list.get(1));
			tmpmap2.put("notIn", list.get(0));
			List<Tmall> tmallList2 = AlibabaDAO.getPreBrand(tmpmap);
			HashSet<Integer> settmp2 = new HashSet<Integer>();
			settmp2.add(list.get(1));
			//System.out.println("+======"+list.get(1));
			for (Tmall tmall : tmallList2) {
				result.put(tmall.getUser_id(), settmp2);
			}
		}
		writeToConsole(result);
		writeToFile(result, "spmf_pre_result.txt");
	}
}
